<?php

namespace App\Http\Controllers\Admin;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class ConsumesController extends Controller
{
    public function consumesList(Request $request){
        $where = array();
        $class_hour_count= null;
        if ($request->isMethod('post')){
            if ($request->name != null){
                array_push($where, [$request->role?'c.name':'d.nice_name', $request->name]); //role=1为学生，=0为老师
            }
            if ($request->date_select != null){
                $date_start = Carbon::parse(substr($request->date, 0, 10));
                $date_end = Carbon::parse(substr($request->date, -10));
                array_push($where, ['class_date', '>=', $date_start]);
                array_push($where, ['class_date', '<=', $date_end]);
            }
            $consume_all = DB::table('consumes as a')
                ->join('classes_logs as b', 'b.id', '=', 'a.classes_log_id')
                ->join('children as c', 'a.child_id', '=', 'c.id')
                ->join('teachers as d', 'd.teacher_id', '=', 'b.teacher_id')
                ->where($where)
                ->get();
            $class_hour = array();
            foreach ($consume_all as $item){
                array_push($class_hour, $item->class_hour);
            }
            $class_hour_count = array_sum($class_hour);
        }
        $consumes = DB::table('consumes as a')
            ->join('classes_logs as b', 'b.id', '=', 'a.classes_log_id')
            ->join('children as c', 'a.child_id', '=', 'c.id')
            ->join('teachers as d', 'd.teacher_id', '=', 'b.teacher_id')
            ->where($where)
            ->select('*', 'a.id as consume_id', 'd.nice_name as teacher_name', 'c.name as child_name')
            ->latest('class_date')
            ->paginate(config('constants.page_num'));
        return view('admin.consumes.index', compact('consumes', 'class_hour_count'));
    }

    public function classesHistory(Request $request){
        if ($request->isMethod('post')){
            $date = Carbon::parse($request->date);
            $week = $date->weekOfYear;
            $year = $date->year;
            $classes_logs = DB::table('classes_logs as c')
                ->join('teachers as t', 'c.teacher_id', '=', 't.teacher_id')
                ->where([['year', $year], ['week', $week]])
                ->select('*', 't.nice_name as teacher_name')
                ->orderBy('class_date')
                ->orderBy('class_time')
                ->get();
            $consumes = DB::table('consumes as a')
                ->join('children as b', 'a.child_id', '=', 'b.id')
                ->whereIn('classes_log_id', DB::table('classes_logs')->where([['year', $year], ['week', $week]])->pluck('id'))
                ->get();
            foreach ($classes_logs as &$classes_log){
                $classes_log->children = null;
                foreach ($consumes as &$consume){
                    if ($classes_log->id == $consume->classes_log_id){
                        $classes_log->children .= $consume->name .',';
                        unset($consume);
                    }
                }
            }
        }else {
            $classes_logs = null;
        }
        return view('admin.consumes.history', compact('classes_logs'));
    }
}
